package com.by.service.impl;

import com.by.common.Utils.Assert;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.entity.ResultBean;
import com.by.common.group.ISave;
import com.by.dao.DeptDao;
import com.by.dao.EmployeeDao;
import com.by.dao.UserDao;
import com.by.entity.Dept;
import com.by.entity.Employee;
import com.by.entity.User;
import com.by.service.DeptService;
import com.by.service.EmployeeService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import static cn.hutool.extra.validation.ValidationUtil.validate;
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee, EmployeeDao> implements EmployeeService {
    @Autowired
    UserDao userDao;

    @Override
    public int delete(Serializable id) {
        //先用id删除用户在删除员工
        int n=userDao.delete(id);
        if(n==1){
            return super.delete(id);
        }
        return 1;
    }

    // 方法内出现异常会回滚
    //五秒内方法未执行报错
    @Transactional(timeout = 5)
    @Override
    public int save(Employee entity) {
        System.out.println(entity.getUser());
        //保存用户
        entity.getUser().setType(1);
        int rs = userDao.save(entity.getUser());
        System.out.println(entity.getUser());
        Assert.isNotSuccess(rs,"用户保存失败");
        return super.save(entity);
    }

}
